﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IndyMosaic.Models.Services;
using System.Web.Security;

namespace IndyMosaic.Models.Services.AspNetServices
{
    public class Membership : IMembershipService
    {

        #region IMembershipService Members

        public int MinPasswordLength
        {
            get { return System.Web.Security.Membership.MinRequiredPasswordLength; }
        }

        public bool ValidateUser(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            return System.Web.Security.Membership.ValidateUser(userName, password);
        }

        public System.Web.Security.MembershipCreateStatus CreateUser(string userName, string password, string confirmPassword,
            string emailAddress, string fullName, string reminderQuestion, string reminderAnswer)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
            if (String.IsNullOrEmpty(confirmPassword)) throw new ArgumentException("Value cannot be null or empty.", "confirmPassword");
            if (String.IsNullOrEmpty(emailAddress)) throw new ArgumentException("Value cannot be null or empty.", "email");

            MembershipCreateStatus status;
            System.Web.Security.Membership.CreateUser(userName, password, emailAddress, reminderQuestion, reminderAnswer, true, out status);

            //CreateProfile(userName, fullName);

            return status;
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword");
            if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword");

            // The underlying ChangePassword() will throw an exception rather
            // than return false in certain failure scenarios.
            try
            {
                MembershipUser currentUser = System.Web.Security.Membership.GetUser(userName, true /* userIsOnline */);
                return currentUser.ChangePassword(oldPassword, newPassword);
            }
            catch (ArgumentException)
            {
                return false;
            }
            catch (MembershipPasswordException)
            {
                return false;
            }
        }

        public string ResetPassword(string userName)
        {
            MembershipUser currentUser = System.Web.Security.Membership.GetUser(userName, false);
            return currentUser.ResetPassword();
        }

        public MembershipUserCollection GetAllUsers(Guid supplierID)
        {
            return System.Web.Security.Membership.GetAllUsers();
        }

        public string GetMembershipCreateStatusInfo(MembershipCreateStatus status)
        {
            string info = "There was an unknown error. Try again.";
            switch (status)
	        {
		        case MembershipCreateStatus.DuplicateEmail:
                    info = "This email is already in our system";
                    break;
                case MembershipCreateStatus.DuplicateUserName:
                    info = "Need to use another email - this one's taken";
                 break;
                case MembershipCreateStatus.InvalidEmail:
                 info = "Invalid email address";
                 break;
                case MembershipCreateStatus.InvalidPassword:
                 info = "Invalid password. Needs to be stronger";
                 break;
                case MembershipCreateStatus.UserRejected:
                 info = "Cannot register at this time";
                 break;
                default:
                 break;
	        }

            return info;
        }
        #endregion

        //private void CreateProfile(string userName, string emailAddress, string firstName, string lastName, string phoneNumber, Guid supplierID)
        //{
        //    UserProfile up = UserProfile.Create(userName);
        //    up.EmailAddress = emailAddress;
        //    up.FirstName = firstName;
        //    up.LastName = lastName;
        //    up.PhoneNumber = phoneNumber;
        //    up.SupplierID = supplierID;
        //    up.Save();
        //}
    }
}
